using System;
using System.Collections.Generic;
using System.Linq;

namespace JVIT.TimeSheet.BLL
{
    [Serializable]
    public class WorkItem : TimedObject
    {
        private string _description;
        public string Description
        {
            get { return _description; }
            set { _description = value; base.ApplyChange(); }
        }
        public Pauses Pauses { get; set; }

        public TimeSpan TotalDuration { get { return this.Duration - this.Pauses.TotalDuration; } } //duration of work item - pauses
        public TimeSpan TotalDurationUnfinished { get { return this.DurationUnfinished - this.Pauses.TotalDurationUnfinished; } } //duration of work item - pauses

        public WorkItem()
        {
            this.Pauses = new Pauses();
        }
        public void Start(string description)
        {
            this.Description = description;
            base.Start();
        }
        public void Pause()
        {
            Pause pause = new Pause();
            pause.Start();
            this.Pauses.Add(pause);
        }
        public void Resume()
        {
            if (this.Pauses.ActivePause != null)
                this.Pauses.ActivePause.Stop();
        }
    }
    [Serializable]
    public class WorkItems : List<WorkItem>
    {
        public WorkState WorkState
        {
            get
            {
                if (this.ActiveWorkItem == null)
                    return BLL.WorkState.Stopped;
                if (this.ActiveWorkItem.Pauses.ActivePause != null)
                    return WorkState.Paused;
                else
                    return BLL.WorkState.Started;
            }
        }
        public WorkItem ActiveWorkItem
        {
            get
            {
                return this.FirstOrDefault(p => p.Started != null && p.Ended == null);
            }
        }

        public TimeSpan Duration
        {
            get
            {
                return new TimeSpan(this.Sum(p => p.Duration.Ticks));
            }
        }

        public TimeSpan DurationUnfinished
        {
            get
            {
                return new TimeSpan(this.Sum(p => p.DurationUnfinished.Ticks));
            }
        }

        public TimeSpan PausesDuration
        {
            get
            {
                return new TimeSpan(this.Sum(p => p.Pauses.TotalDuration.Ticks));
            }
        }

        public TimeSpan PausesDurationUnfinished
        {
            get
            {
                return new TimeSpan(this.Sum(p => p.Pauses.TotalDurationUnfinished.Ticks));
            }
        }

        public TimeSpan TotalDuration
        {
            get
            {
                return new TimeSpan(this.Sum(p => p.Duration.Ticks) - this.Sum(p => p.Pauses.TotalDuration.Ticks));
            }
        }

        public TimeSpan TotalDurationUnfinished
        {
            get
            {
                return new TimeSpan(this.Sum(p => p.DurationUnfinished.Ticks) - this.Sum(p => p.Pauses.TotalDurationUnfinished.Ticks));
            }
        }

        public DateTime? Started { get { return this.Count > 0 ? this.First().Started : null; } }
        public DateTime? Ended { get { return this.Count > 0 ? this.Last().Ended : null; } }

        public void Start(string description)
        {
            WorkItem workItem = new WorkItem();
            workItem.Start(description);
            this.Add(workItem);
        }
        public void Stop()
        {
            this.ActiveWorkItem.Stop();
        }
        public void Pause()
        {
            this.ActiveWorkItem.Pause();
        }
        public void Resume()
        {
            this.ActiveWorkItem.Resume();
        }
    }
}
